package org.phantom.securityframework.api.subject;

import org.apache.shiro.authc.AuthenticationToken;

import org.phantom.securityframework.api.crypt.CryptUtil;
import org.phantom.securityframework.api.crypt.Encrypt;
/**
 * 帐号身份
 * @author david
 *
 */
@SuppressWarnings("serial")
public class AccountToken implements AuthenticationToken {

	/**
	 * 帐号
	 */
	private String account;
	/**
	 * 密码
	 */
	private String password;
	/**
	 * 密码加密策略
	 */
	private Encrypt encrypt;
	
	private boolean encoded = false;
	
	
	public AccountToken() {
		super();
	}
	public AccountToken(String account, String password) {
		super();
		this.account = account;
		this.password = password;
	}
	public AccountToken(String account, String password,Encrypt encrypt) {
		super();
		this.account = account;
		this.password = password;
		this.encrypt = encrypt;
	}
	public String getAccount() {
		return account;
	}
	public void setAccount(String account) {
		this.account = account;
	}
	public String getPassword() {
		if(this.encrypt!=null){
			try {
				if(!this.encoded){
					this.password = CryptUtil.encodeBytes(this.encrypt.encrypt(password.getBytes(), null));
					this.encoded = true;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public Object getPrincipal() {
		return account;
	}
	public Object getCredentials() {
		return this.getPassword();
	}
	public Encrypt getEncrypt() {
		return encrypt;
	}
	public void setEncrypt(Encrypt encrypt) {
		this.encrypt = encrypt;
	}
}
